package com.ciis.system.relation.service.impl;

import com.ciis.system.relation.domain.TSysRoleAuthority;
import com.ciis.system.relation.mapper.ITSysRoleAuthorityMapper;
import com.ciis.system.relation.service.ISysRoleAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zhangdajun
 * Date: 14-5-31
 * Time: 下午3:10
 * To change this template use File | Settings | File Templates.
 */
@Service("roleAuthorityService")
public class SysRoleAuthorityServiceImpl implements ISysRoleAuthorityService {
    @Autowired
    ITSysRoleAuthorityMapper roleAuthorityMapper;
    @Override
    public List<TSysRoleAuthority> getAuthorityByRId(String roleId) throws Exception {
        return roleAuthorityMapper.getAuthorityByRId(roleId);
    }

    @Override
    public List<TSysRoleAuthority> getAuthorityByUId(String userId) throws Exception {
        return roleAuthorityMapper.getAuthorityByUId(userId);
    }

    @Override
    public HashMap<String, TSysRoleAuthority> getAuthorityMapByRId(String roleId) throws Exception {
        List<TSysRoleAuthority> roleAuthorityList = getAuthorityByRId(roleId);
        return buildRoleMap(roleAuthorityList);
    }

    @Override
    public HashMap<String, TSysRoleAuthority> getAuthorityMapByUId(String userId) throws Exception {
        List<TSysRoleAuthority> roleAuthorityList = getAuthorityByUId(userId);
        return buildRoleMap(roleAuthorityList);
    }

    private HashMap<String, TSysRoleAuthority> buildRoleMap(List<TSysRoleAuthority> roleAuthorities){
        HashMap<String, TSysRoleAuthority> authorityHashMap = new HashMap<String, TSysRoleAuthority>();
        if(roleAuthorities != null && roleAuthorities.size() > 0){
            for(TSysRoleAuthority roleAuthority : roleAuthorities){
                authorityHashMap.put(roleAuthority.getAuth_id(),roleAuthority);
            }
        }
        return authorityHashMap;
    }
}
